package com.yupi.springbootinit.config;

import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 *自定义线程池
 */
@Configuration
public class ThreadPoolTaskExecutorConfig {
    /**
     *
     * @return
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        //线程工厂
        ThreadFactory threadFactory = (runnable)->{
            int count = 1;
            //创建一个新的线程
            Thread thread = new Thread(runnable);
            thread.setName("线程："+count);
            count++;
            return thread;
        };

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,4,60000,
                TimeUnit.SECONDS,new ArrayBlockingQueue<>(4),threadFactory);
        return threadPoolExecutor;
    }
}
